package com.wpre.booksystem.service;

import com.wpre.booksystem.enums.BookStatusEnum;
import com.wpre.booksystem.mapper.BookMapper;
import com.wpre.booksystem.model.BookInfo;
import com.wpre.booksystem.model.PageRequest;
import com.wpre.booksystem.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-10-25
 * Time: 20:07
 */
@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;


    //插入图书数据
    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    //    查询图书信息(翻页功能)
    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {

        //1.总的记录数
        Integer count = bookMapper.count();
        //2.当前页的记录
        List<BookInfo> bookInfoList = bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());

        //设置
        for (int i = 0; i < bookInfoList.size(); i++) {
            Integer status = bookInfoList.get(i).getStatus();
            if (status == 0) {
                bookInfoList.get(i).setStatusCN(BookStatusEnum.DELETED.getStatus());
            } else if (status == 1) {
                bookInfoList.get(i).setStatusCN(BookStatusEnum.NORMAL.getStatus());
            } else {
                bookInfoList.get(i).setStatusCN(BookStatusEnum.FORBIDDEN.getStatus());
            }
        }
//        System.out.println("总记录数" + count);
//        System.out.println("书名" + bookInfoList.get(1).getBookName());
//        System.out.println("状态" + bookInfoList.get(1).getStatusCN());
        return new PageResponse<BookInfo>(count, bookInfoList, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    //更新图书
    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    //批量删除
    public void batchDelete(List<Integer> ids) {
        bookMapper.batchDelete(ids);
    }
}
